package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.OrderdetailM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 订单详情
 */
public class OrderDetailsActivity extends BaseActivity {

    @BindView(R.id.li_orderdetail_list)
    LinearLayout liOrderdetailList;
    @BindView(R.id.tv_order_allprice)
    TextView tvOrderAllprice;
    @BindView(R.id.tv_orderdetail_title)
    TextView tvOrderdetailTitle;
    @BindView(R.id.tv_detail_topay)
    TextView tvDetailTopay;
    private String orderId;
    private OrderdetailM model;
    ArrayList<OrderdetailM.ObjectBean.OrderDetailsBean> Temp_list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        ButterKnife.bind(this);
        ChangLayTitle("订单明细");
        LayBack();
        orderId = getIntent().getStringExtra("orderId");
        if (!TextUtils.isEmpty(getIntent().getStringExtra("FromDD"))) {//我的订单---未支付
            tvDetailTopay.setVisibility(View.VISIBLE);
        }
        getData();
    }

    /**
     * 获取订单明细
     */
    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.MyOrderdetails, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(OrderDetailsActivity.this, "uid"));
        mRequest.add("orderId", orderId);
        // 添加到请求队列
        CallServer.getRequestInstance().add(OrderDetailsActivity.this, 0, mRequest,
                new CustomHttpListener(OrderDetailsActivity.this, true, OrderdetailM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                         model = ((OrderdetailM) data);
                        tvOrderdetailTitle.setText(model.getObject().getSubject());
                        tvOrderAllprice.setText("合计：¥ " + model.getObject().getTotalAmount());
                        Temp_list.addAll(model.getObject().getOrderDetails());
                        for (int i = 0; i < model.getObject().getOrderDetails().size(); i++) {
                            View v = getLayoutInflater().inflate(R.layout.item_orderdatils, null);
                            TextView name = (TextView) v.findViewById(R.id.tv_orderdetail_name);
                            TextView nums = (TextView) v.findViewById(R.id.tv_orderdetail_nums);
                            TextView price = (TextView) v.findViewById(R.id.tv_orderdetail_price);
                            View ban = (View) v.findViewById(R.id.view_orderdetail_ban);
                            View man = (View) v.findViewById(R.id.view_orderdetail_man);
                            name.setText(model.getObject().getOrderDetails().get(i).getTitle());
                            nums.setText("x" + model.getObject().getOrderDetails().get(i).getProNum());
                            price.setText( "¥"+model.getObject().getOrderDetails().get(i).getProPrice());
                            if (i == model.getObject().getOrderDetails().size() - 1) {
                                ban.setVisibility(View.GONE);
                                man.setVisibility(View.VISIBLE);
                            } else {
                                ban.setVisibility(View.VISIBLE);
                                man.setVisibility(View.GONE);
                            }
                            liOrderdetailList.addView(v);
                        }


                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(OrderDetailsActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }

    public void doClick(View v) {
        Intent in = new Intent(OrderDetailsActivity.this, SureDDActivity.class);
        Bundle map = new Bundle();
        map.putSerializable("gwc_list", Temp_list);
        in.putExtras(map);
        in.putExtra("FromDD", "1");
        in.putExtra("orderId",orderId);
        startActivity(in);
    }
}
